package org.aeon.utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

public class SwingUtil {
    private static final AffineTransform atf = new AffineTransform();

    private static final FontRenderContext frc = new FontRenderContext(atf, true, true);

    /**
     * 获取字符串的高度
     *
     * @param str
     * @param font
     * @return
     */
    public static int getStringHeight(String str, Font font) {
        if (str == null || str.isEmpty() || font == null) {
            return 0;
        }
        return (int) font.getStringBounds(str, frc).getHeight();

    }

    /**
     * 获取字符串的宽度
     *
     * @param str
     * @param font
     * @return
     */
    public static int getStringWidth(String str, Font font) {
        if (str == null || str.isEmpty() || font == null) {
            return 0;
        }
        return (int) font.getStringBounds(str, frc).getWidth();
    }

    /**
     * 修改组件所有大小
     *
     * @param component
     * @param dimension
     */
    public static void setSize(Component component, Dimension dimension) {
        component.setSize(dimension);
        component.setPreferredSize(dimension);
        component.setMaximumSize(dimension);
        component.setMinimumSize(dimension);
    }

    /**
     * 创建一个圆角面板
     *
     * @return
     */
    public static JPanel roundedPanel() {
        return new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                // super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.WHITE);
                g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 10, 10);
                g2d.dispose();
            }
        };
    }

    /**
     * 添加一个点击获取焦点事件
     *
     * @param component
     */
    public static void addGetFocusEvent(Component component) {
        component.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                component.requestFocus();
            }
        });
    }

    /**
     * 去除按钮样式
     *
     * @param button
     */
    public static void removeStyle(AbstractButton button) {
        // 去除边框
        button.setBorderPainted(false);
        // 去除填充
        button.setContentAreaFilled(false);
        // 禁用按钮获取焦点时的虚线框
        button.setFocusPainted(false);
        // 禁用按钮的焦点功能
        button.setFocusable(false);
    }

    /**
     * 去除按钮样式
     *
     * @param button
     */
    public static void removeStyle2(AbstractButton button) {
        // 去除填充
        button.setContentAreaFilled(false);
        // 禁用按钮获取焦点时的虚线框
        button.setFocusPainted(false);
    }

    /**
     * 创建遮罩
     *
     * @param width
     * @param height
     * @return
     */
    public static JPanel createMask(int width, int height) {
        final JPanel mask = new JPanel();
        mask.setLayout(null);
        mask.setOpaque(true);
        mask.setBackground(new Color(0x882D2D2D, true));
        mask.setBounds(0, 1, width, height);
        return mask;
    }
}
